<?php

namespace App\Http\Controllers;


use App\Chest;
use App\Group;
use App\SumIncome;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class UserController extends Controller
{
    public function __construct()
    {
        //$this->middleware('sms')->only('store');
    }

    public function index(Request $request)
    {
        $user = Auth::user();
        $fee = SumIncome::where(['user_id' => $user->id])->first();
        $amount = $user->income()->sum('fee');
        $chestNumber ='869300038720094';//$request->get('chest_number');
        if($chestNumber || $user->isUser())
        {
            $request->get('chest_number');
            $groups = $user->group()->get();
            $chest = Chest::where('chest_number', $chestNumber)->first();
            if (!$chest) {
                return $this->error('柜子编号错误');
            }
            return view('user.index', compact('user', 'amount', 'chest', 'groups'));
        }else{
            return view('company.index', compact('user', 'amount','fee','groups'));
        }
    }
    public function list()
    {
        $user = Auth::user();
        $groups = $user->group()->get();
        $isCompany = false;
        foreach ($groups as $group) {
            if ($group->isCompany()) {
                $isCompany = true;
                break;
            }

        }
        return view('user.member.index', compact('user', 'groups', 'isCompany'));
    }

    public function create()
    {
        $user = Auth::user();
        return view('user.member.create', compact('user'));
    }

    public function store(Request $request)
    {
        $this->validate($request, [
            'group_id' => 'required|integer',
            'phone' => 'required'
        ]);
        $user = Auth::user();
        $groupId = $request->get('group_id');
        $group = Group::find($groupId);
        $phone = $request->get('phone');
        $registerUser = User::where('phone', $phone)->first();
        if (empty($registerUser))
        {
            return $this->error('被添加用户未注册，请注册后重试', route('user.create'));
        } else {
            if ($user->canAddMember($group, $registerUser)) {
                $registerUser->groups()->attach($group);
                return $this->success('添加成员成功', route('user.index'));
            } else {
                return $this->error('用户添加失败',route('user.index'));
            }
        }
    }


    public function edit()
    {
        // 修改成员  to do
    }

    public function destroy(Request $request)
    {

        $this->validate($request, [
            'group_id' => 'required|exists:groups,id',
            'user_id' => 'required|exists:users,id'
        ]);

        $user = Auth::user();
        $userId = $request->get('user_id');
        $groupId = $request->get('group_id');

        $group = Group::find($groupId);

        if (!$user->hasGroup($group)) {
            //如果改用户组不是该用户创建的  那么不允许删除
            return $this->error('当前组不属于当前用户');
        }

        if (!$group) {
            return $this->error('用户组不存在');
        }

        $user = User::find($userId);

        if ($group->users()->detach($user)) {
            return $this->success('删除成员成功', route('user.index'));
        } else {
            return $this->error('删除成员失败', route('user.index'));
        }

    }

    public function orderDetail()
    {
        //时租用户的订单详情
        $user = Auth::user();
        return view('wallet.timeOrder', compact('user'));
    }
}
